﻿using Black.NBT;
using System.Windows;
using System.Collections.Generic;

namespace BlackNBT.ClientTest
{
    /// <summary>
    /// Formulaire de modification d'une Liste
    /// </summary>
    public partial class EditList : Window, IDataEditWindows
    {
        #region Propriétés

        /// <summary>
        /// Obtient ou définit la donnée saisie
        /// </summary>
        public TagList Data
        {
            get;
            set;
        }

        #endregion

        #region Constructeur

        /// <summary>
        /// Constructeur de la classe
        /// </summary>
        public EditList()
        {
            this.InitializeComponent();
            this.InitItems();
        }

        /// <summary>
        /// Constructeur de la classe
        /// </summary>
        /// <param name="data">Donnée a modifier</param>
        public EditList(TagList data)
        {
            this.InitializeComponent();
            this.InitItems();

            this.Data = data;
            this.Title.Text = "Modify Entry";
            this.ItemName.Text = data.Name;

            this.ItemType.SelectedItem = data.Type;
        }

        #endregion

        #region Fonctions publiques

        /// <summary>
        /// Récupère le tag
        /// </summary>
        /// <returns>Instance du tag</returns>
        public Tag GetTag()
        {
            return ((Tag)this.Data);
        }

        #endregion

        #region Fonctions privées

        /// <summary>
        /// Charge la liste des types de tag
        /// </summary>
        private void InitItems()
        {
            List<TagType> list = new List<TagType>();

            list.Add(TagType.Byte);
            list.Add(TagType.ByteArray);
            list.Add(TagType.Compound);
            list.Add(TagType.Double);
            list.Add(TagType.Float);
            list.Add(TagType.Int);
            list.Add(TagType.List);
            list.Add(TagType.Long);
            list.Add(TagType.Short);
            list.Add(TagType.String);

            this.ItemType.ItemsSource = list;
        }

        /// <summary>
        /// Se produit sur le bouton Cancel
        /// </summary>
        /// <param name="sender">Source de l'appel</param>
        /// <param name="e">Argument de l'appel</param>
        private void OnCancelClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        /// <summary>
        /// Se produit sur le bouton Save
        /// </summary>
        /// <param name="sender">Source de l'appel</param>
        /// <param name="e">Argument de l'appel</param>
        private void OnSaveClick(object sender, RoutedEventArgs e)
        {
            // Vérification du nom

            if (string.IsNullOrWhiteSpace(this.ItemName.Text))
            {
                MessageBox.Show("You must enter a name.", "Data Verification", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            // Vérification du type

            if (this.ItemType.SelectedItem == null)
            {
                MessageBox.Show("You must enter a type.", "Data Verification", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            // Modification

            if (this.Data == null)
                this.Data = new TagList(string.Empty);
            else
            {
                if (this.Data.Type != (TagType)this.ItemType.SelectedItem)
                {
                    if (MessageBox.Show("You changed the TagList type with a different type of his origin.\nThis action should delete all children items.\nDo you want to continue ?", "Type modification", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) != MessageBoxResult.Yes)
                        return;

                    this.Data.Items.Clear();
                }
            }

            // Affectation des données

            this.Data.Name = this.ItemName.Text;
            this.Data.Type = (TagType)this.ItemType.SelectedItem;

            // On ferme la fenêtre

            this.DialogResult = true;
            this.Close();
        }

        #endregion
    }
}
